package com.dushiguang.findhoney;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dushiguang.findhoney.View.CircleIndicator.CirclePageIndicator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 首期使用的欢迎界面
 * Created by ex-dushiguang201 on 2015-11-13.
 */
@EActivity(R.layout.activity_guide)
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener{

    @ViewById(R.id.guide_viewPager)
    ViewPager mGuideViewPager;
    @ViewById(R.id.guide_indicator)
    CirclePageIndicator mGuideIndicator;

    private static final int[] welcomeImages = new int[] {R.mipmap.welcome_01,R.mipmap.welcome_02,R.mipmap.welcome_03};
    private int lastIndex = welcomeImages.length - 1;

    private boolean isAbout = false;
    private boolean isScrolling = false;
    private boolean isScrollToNext = true; //确保只滑动finish一次
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @AfterViews
    void initViews() {
        List<View> listViews = new ArrayList<View>();

        int i;
        int length = welcomeImages.length;
        for(i = 0; i < length; i++) {
            View view;
            ImageView imageView;
            view = getLayoutInflater().inflate(R.layout.welcomeview, null);
            imageView = (ImageView)view.findViewById(R.id.guide_image);
            imageView.setImageResource(welcomeImages[i]);

            listViews.add(view);
        }

        initIndicator();
        mGuideViewPager.setAdapter(new ViewPagerAdapter(listViews));
        mGuideIndicator.setViewPager(mGuideViewPager);
        mGuideIndicator.setOnPageChangeListener(this);
    }

    private void initIndicator() {
        mGuideIndicator.setRadiusRatio(5);//设置引导圆圈的半径
        mGuideIndicator.setFillColor(getResources().getColor(R.color.colorAccent));//设置指示当前页面的实心圆圈的颜色
        mGuideIndicator.setPageColor(getResources().getColor(R.color.colorPrimary));//设置其他页面的指示圆点
        mGuideIndicator.setStrokeWidth(0);
    }

    private void endActivity() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(position == lastIndex && isScrolling && isScrollToNext) {
            isScrollToNext = false;
            endActivity();
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_DRAGGING) {
            isScrolling = true;
        } else {
            isScrolling = false;
        }
    }

    private class ViewPagerAdapter extends PagerAdapter{
        private List<View> viewPager;
        public ViewPagerAdapter(List<View> viewPager) {
            this.viewPager = viewPager;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewPager.get(position));//删除页
        }

        @Override
        public int getCount() {
            if(viewPager.isEmpty()) {
                return 0;
            }
            return viewPager.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewPager.get(position), 0);
            return viewPager.get(position);
        }
    }

    public static void jumpToGuideActivity (Context context) {
        Intent intent = new Intent(context, GuideActivity_.class);
        context.startActivity(intent);
    }
}
